<?php
/**
 * Fastapp.
 * 3/7/24
 * @Author deepsea
 * @Contact telegram:deepsea159 
 */

namespace App\Fastapp;

use Hyperf\Filesystem\Contract\AdapterFactoryInterface;
use Hyperf\Filesystem\FilesystemFactory;
use League\Flysystem\Filesystem;

class FastappFilesystemFactory extends FilesystemFactory
{
    public function get($adapterName): Filesystem
    {
        $key = match ($adapterName) {
            'cos' => 'qqoss',
            'oss' => 'alioss',
            'qiniu' => 'qiniuoss',
            default => 'local'
        };

        $res = api_cache()->getConfigByGroupKey('upload_config');
        $options = [];
        if ($adapterName == 'local') {
            $options['root'] = BASE_PATH . '/public/' . env('UPLOAD_PATH', 'uploadfile');
        } else {
            foreach ($res as $k => $v) {
                if (str_starts_with($k, $key)) $options[str_replace($key . '_', '', $k)] = $v;
            }
        }
        /** @var AdapterFactoryInterface $driver */
        $driver = container()->get(config("file.storage.$adapterName.driver"));
        return new Filesystem($driver->make($options), $options);
    }
}